package com.edgclub.edg.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.edgclub.edg.R;
import com.edgclub.edg.activity.ImageViewPagerActivity;
import com.edgclub.edg.adapter.adapteruitls.GzBaseAdapter;
import com.edgclub.edg.adapter.adapteruitls.MyItemClickListener;
import com.edgclub.edg.adapter.adapteruitls.MyItemLongClickListener;
import com.edgclub.edg.adapter.adapteruitls.RViewHolder;
import com.edgclub.edg.model.PostsAlbum;
import com.edgclub.edg.utils.GlideUtil.GlideUtils;

import java.util.ArrayList;

/**
 */
public class BBDetailImageAdapter extends GzBaseAdapter {
    private Context mContext;

    private ArrayList<PostsAlbum> postsAlbums;

    private GlideUtils glideUtils;

    private boolean isShowPlay = false;

    public BBDetailImageAdapter(Context mContext) {
        this.mContext = mContext;
        glideUtils = new GlideUtils();
    }

    public ArrayList<PostsAlbum> getPostsAlbums() {
        return postsAlbums;
    }

    public void setPostsAlbums(ArrayList<PostsAlbum> postsAlbums) {
        this.postsAlbums = postsAlbums;
    }


    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_bb_image,viewGroup,false);
        ViewHolder vh = new ViewHolder(view,mListener,mLongClickListener);
        return vh;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ViewHolder viewHolder = (ViewHolder) holder;
        final PostsAlbum postsAlbum = postsAlbums.get(position);
        ViewGroup.LayoutParams params = viewHolder.image.getLayoutParams();
        params.width = postsAlbum.getWidth();
        params.height = postsAlbum.getHeight();
        viewHolder.image.setLayoutParams(params);
        viewHolder.play.setVisibility(View.GONE);
        if (null != postsAlbum && postsAlbum.getThumbnail() != null) {
            glideUtils.Glidde_loadImage(mContext, postsAlbum.getThumbnail(), viewHolder.image);
        }

        viewHolder.image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext,
                        ImageViewPagerActivity.class);
                intent.putExtra("urls", postsAlbums);
                intent.putExtra("position", position);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                mContext.startActivity(intent);
                ((Activity) mContext).overridePendingTransition(R.anim.head_in, R.anim.head_out);
            }
        });

    }

    @Override
    public long getItemId(int arg0) {
        return 0;
    }

    @Override
    public int getItemCount() {
        return postsAlbums.size();
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RViewHolder {
        public ImageView image,play;

        public ViewHolder(View view, MyItemClickListener mListener, MyItemLongClickListener mLongClickListener){
            super(view,mListener,mLongClickListener);
            image = (ImageView) view.findViewById(R.id.image);
            play = (ImageView) view.findViewById(R.id.image_play);

        }
    }

}
